// 配置axios的基础路径
axios.defaults.baseURL = "http://api-breakingnews-web.itheima.net";

// 请求拦截器
axios.interceptors.request.use(function(config) {
    if (config.url.includes('/my/')) {
        config.headers.Authorization = localStorage.getItem("token")
    }
    return config;
}, function(error) {
    return Promise.reject(error)
})

// 响应拦截器
axios.interceptors.response.use(function(response) {
    let obj = response.data
    if (obj.message === "身份认证失败！") {
        location.href = "/login.html"
        localStorage.removeItem("token")
    }
    return response;
}, function(error) {
    return Promise.reject(error)
})

// 日期格式化
function dataFormat(str) {
    let dt = new Date(str);
    let yy = dt.getFullYear();
    let mm = padZero(dt.getMonth() + 1);
    let dd = padZero(dt.getDate());

    let h = padZero(dt.getHours());
    let m = padZero(dt.getMinutes());
    let s = padZero(dt.getSeconds());

    return `${yy}-${mm}-${dd} ${h}:${m}:${s}`
}
// 日期补0
function padZero(n) {

    return n < 10 ? "0" + n : n
}